package pojo.subjectentity;

import Mysqlcon.Conn;

import java.sql.*;
import java.util.Scanner;

public class subjectTitle extends Conn {

    //标签菜单
    public void menu() throws Exception {

        while (true) {
            System.out.println("******欢迎来到标签管理******");
            System.out.println("1.添加标签");
            System.out.println("2.查看标签列表");
            System.out.println("3.删除指定标签");
            System.out.println("4.退       出");
            System.out.print("请输入:");
            Scanner sc = new Scanner(System.in);

            int i = sc.nextInt();

            if (i == 1) {
                insertSubject();
            } else if (i == 2) {
                showSubject();
            } else if (i == 3) {
                deleteSubject();
            }else{
                break;
            }
        }
    }
    //插入标签
    public void insertSubject() throws Exception {
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("******欢迎来到添加标签******");
            System.out.print("请输入标签名称:");
            String title = sc.nextLine();

            String sql = "insert into title values(?,NOW())";
            prepareStatement = connection.prepareStatement(sql);
            prepareStatement.setString(1,title);
            int num = prepareStatement.executeUpdate();
            if (num > 0) {
                System.out.println("恭喜，标签添加成功！");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    //展示标签
    public void showSubject() throws Exception {
        try{
            System.out.println("******欢迎来到课程列表展示******");
            sta = connection.createStatement();

            String sql = "select subjectname,inserttime from title";

            ResultSet rs = sta.executeQuery(sql);

            while(rs.next()){
                System.out.println("标签名称:"+rs.getString("subjectname")+";创建时间:"+rs.getString("inserttime"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    //删除标签
    public void deleteSubject() throws Exception {
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("******欢迎来到删除标签******");
            System.out.print("请输入标签名称:");
            String title = sc.nextLine();

            String sql = "delete from title where subjectname = ?";
            prepareStatement = connection.prepareStatement(sql);
            prepareStatement.setString(1,title);
            int num = prepareStatement.executeUpdate();
            if (num > 0) {
                System.out.println("恭喜，删除成功！");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
